package com.chunguang.netty.netty.clinet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

public class Client {

	public static void main(String[] args) {
		Bootstrap serverBootstrap = new Bootstrap();
		EventLoopGroup worker = new NioEventLoopGroup();
		serverBootstrap.group( worker);
		
		serverBootstrap.channel(NioSocketChannel.class);
		serverBootstrap.handler(new ChannelInitializer<Channel>() {

			@Override
			protected void initChannel(Channel ch) throws Exception {
				ch.pipeline().addLast(new StringDecoder()) ;
				ch.pipeline().addLast(new StringEncoder()) ;
				ch.pipeline().addLast(new HiHandler()) ;
			}
		});
		
		ChannelFuture connect = serverBootstrap.connect(new InetSocketAddress("127.0.0.1", 8000));
		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
		Channel channel = connect.channel();
		while(true){
			System.out.println("请输入：");
			String msg = null;
			try {
				msg = bufferedReader.readLine();
				channel.writeAndFlush(msg);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
	}
}
